<?php

namespace App\Http\Controllers\Product;

use App\Helpers\Code;
use App\Models\Product\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class ProductController extends Controller
{

    /**
     * ProductController constructor.
     *
     * @author lql
     */
    public function __construct()
    {
        $this->model = new Product();

        $this->_validators();
    }

    /**
     * 添加产品
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function addProduct(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addProduct($data);
            $this->checked($model, [
                'null' => 'product.product.test_method_not_exists',
                Code::INSIDE_ERROR => 'product.product.product_attribute_not_exists',
                Code::INSIDE_ERROR_1 => 'product.product.sequence_platform_type_not_exists',
                Code::INSIDE_ERROR_2 => 'product.product.way_not_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑产品 todo：后期应需求做更改
     * @param Request $request
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function editProduct(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['created_by', 'data_scope']));

            $rules = [];
            foreach ($data as $key => $value) {
                switch ($key) {
                    case 'code':
                        $rules[$key] = ['bail', 'required', \Illuminate\Validation\Rule::unique('products')->ignore($id)];
                        break;
                    case 'remark':
                        break;
                    default:
                        $rules[$key] = $this->rules[$key];
                        break;
                }
            }

            $validator = Validator::make($data, $rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editProduct($data, $id);
            $this->checked($model, [
                'null' => 'product.product.product_or_parent_not_exists',
                Code::INSIDE_ERROR => 'product.product.test_method_not_exists',
                Code::INSIDE_ERROR_1 => 'product.product.product_attribute_not_exists',
                Code::INSIDE_ERROR_2 => 'product.product.sequence_platform_type_not_exists',
                Code::INSIDE_ERROR_3 => 'product.product.way_not_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取列表 todo:后期根据前端需求更改返回字段、目前全部信息返回
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function getProductList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int)$request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $defaultSorter = $request->input('sorter') ?? 'desc';

            return preprocess($this->model->getProductList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取产品树
     * @return array
     */
    public function getProductTree()
    {
        try {
            $this->result['data'] = $this->model->getProductTree();

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getProduct()
    {
        try {

        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getProductPackageAll()
    {
        try {

        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'parent_id' => 'bail|nullable|numeric|exists:products,id',
            'title' => 'bail|required|max:100',
            'code' => 'bail|required|unique:products',
            'test_method' => 'bail|nullable|max:200',
            'price' => 'bail|nullable|max:200',
            'is_urgent' => 'bail|required|in:0,1',
            'is_package' => 'bail|required|in:0,1',
            'is_genetic_disease' => 'bail|required|in:0,1',
            'is_genetic_diagnosis' => 'bail|required|in:0,1',
            'is_family' => 'bail|required|in:0,1',
            'is_contain_family' => 'bail|required|in:0,1',
            'is_more_monitor' => 'bail|required|in:0,1',
            'is_alone_order' => 'bail|required|in:0,1',
            'is_report' => 'bail|required|in:0,1',
            'state' => 'bail|required|in:0,1',
            'clinical_speciality' => 'bail|nullable|max:200',
            'clinical_significance' => 'bail|nullable|max:200',
            'modeless_name' => 'bail|nullable|max:200',
            'sequencing_strategy' => 'bail|nullable|max:200',
            'group_special_test' => 'bail|nullable|max:200',
            'member_add_price' => 'bail|nullable|max:200',
            'analytic_strategy' => 'bail|nullable|max:200',
            'execute_kind' => 'bail|nullable|max:200',
            'analytic_variation_type' => 'bail|nullable|max:200',
            'sample_type_desc' => 'bail|nullable|max:200',
            'sample_transport' => 'bail|nullable|max:200',
            'free_verification_num' => 'bail|nullable|max:200',
            'point_variation_method' => 'bail|nullable|max:200',
            'point_variation_price' => 'bail|nullable|max:200',
            'cnv_variation_method' => 'bail|nullable|max:200',
            'cnv_variation_price' => 'bail|nullable|max:200',
            'report_period' => 'bail|nullable|max:200',
            'ngs_period' => 'bail|nullable|max:200',
            'product_attribute' => 'bail|nullable|max:200',
            'monitor_param' => 'bail|nullable|max:200',
            'morepcr_primer' => 'bail|nullable|max:200',
            'capture_chip' => 'bail|nullable|max:200',
            'sequencing_depth' => 'bail|nullable|max:200',
            'single_carry_code' => 'bail|nullable|max:200',
            'single_carry_name' => 'bail|nullable|max:200',
            'library_type' => 'bail|nullable|max:200',
            'method' => 'bail|nullable|max:200',
            'lci_index' => 'bail|nullable|max:200',
            'si_index' => 'bail|nullable|max:200',
            'insert_size' => 'bail|nullable|max:200',
            'target_yield' => 'bail|nullable|max:200',
            'qc_requirements' => 'bail|nullable|max:200',
            'sequence_platform' => 'bail|nullable|max:200',
            'sequence_type' => 'bail|nullable|max:200',
            'sequence_length' => 'bail|nullable|max:200',
            'client_index' => 'bail|nullable|max:200',
            'phix' => 'bail|nullable|max:200',
            'delivery_method' => 'bail|nullable|max:200',
            'data_analysis' => 'bail|nullable|max:200',
            'analysis_details' => 'bail|nullable|max:200',
            'flux' => 'bail|nullable|max:200',
            'discount' => 'bail|nullable|max:200',
            'product_fee' => 'bail|nullable|max:200',
            'way' => 'bail|nullable|max:200',
        ];

        $this->messages = [
            'parent_id.numeric' => trans('product.product.parent_id_numeric'),
            'parent_id.exists' => trans('product.product.parent_id_exists'),
            'title.required' => trans('product.product.title_required'),
            'title.max' => trans('product.product.title_max'),
            'code.required' => trans('product.product.code_required'),
            'code.unique' => trans('product.product.code_unique'),
            'test_method.max' => trans('product.product.test_method_max'),
            'price.max' => trans('product.product.price_max'),
            'is_urgent.required' => trans('product.product.is_urgent_required'),
            'is_urgent.in' => trans('product.product.is_urgent_in'),
            'is_package.required' => trans('product.product.is_package_required'),
            'is_package.in' => trans('product.product.is_package_in'),
            'is_genetic_disease.required' => trans('product.product.is_genetic_disease_required'),
            'is_genetic_disease.in' => trans('product.product.is_genetic_disease_in'),
            'is_genetic_diagnosis.required' => trans('product.product.is_genetic_diagnosis_required'),
            'is_genetic_diagnosis.in' => trans('product.product.is_genetic_diagnosis_in'),
            'is_family.required' => trans('product.product.is_family_required'),
            'is_family.in' => trans('product.product.is_family_in'),
            'is_contain_family.required' => trans('product.product.is_contain_family_required'),
            'is_contain_family.in' => trans('product.product.is_contain_family_in'),
            'is_more_monitor.required' => trans('product.product.is_more_monitor_required'),
            'is_more_monitor.in' => trans('product.product.is_more_monitor_in'),
            'is_alone_order.required' => trans('product.product.is_alone_order_required'),
            'is_alone_order.in' => trans('product.product.is_alone_order_in'),
            'is_report.required' => trans('product.product.is_report_required'),
            'is_report.in' => trans('product.product.is_report_in'),
            'state.required' => trans('product.product.state_required'),
            'state.in' => trans('product.product.state_in'),
            'clinical_speciality.max' => trans('product.product.clinical_speciality_max'),
            'clinical_significance.max' => trans('product.product.clinical_significance_max'),
            'modeless_name.max' => trans('product.product.modeless_name_max'),
            'sequencing_strategy.max' => trans('product.product.sequencing_strategy_max'),
            'group_special_test.max' => trans('product.product.group_special_test_max'),
            'member_add_price.max' => trans('product.product.member_add_price_max'),
            'analytic_strategy.max' => trans('product.product.analytic_strategy_max'),
            'execute_kind.max' => trans('product.product.execute_kind_max'),
            'analytic_variation_type.max' => trans('product.product.analytic_variation_type_max'),
            'sample_type_desc.max' => trans('product.product.sample_type_desc_max'),
            'sample_transport.max' => trans('product.product.sample_transport_max'),
            'free_verification_num.max' => trans('product.product.free_verification_num_max'),
            'point_variation_method.max' => trans('product.product.point_variation_method_max'),
            'point_variation_price.max' => trans('product.product.point_variation_price_max'),
            'cnv_variation_method.max' => trans('product.product.cnv_variation_method_max'),
            'cnv_variation_price.max' => trans('product.product.cnv_variation_price_max'),
            'report_period.max' => trans('product.product.report_period_max'),
            'ngs_period.max' => trans('product.product.ngs_period_max'),
            'product_attribute.max' => trans('product.product.product_attribute_max'),
            'monitor_param.max' => trans('product.product.monitor_param_max'),
            'morepcr_primer.max' => trans('product.product.morepcr_primer_max'),
            'capture_chip.max' => trans('product.product.capture_chip_max'),
            'sequencing_depth.max' => trans('product.product.sequencing_depth_max'),
            'single_carry_code.max' => trans('product.product.single_carry_code_max'),
            'single_carry_name.max' => trans('product.product.single_carry_name_max'),
            'library_type.max' => trans('product.product.library_type_max'),
            'method.max' => trans('product.product.method_max'),
            'lci_index.max' => trans('product.product.lci_index_max'),
            'si_index.max' => trans('product.product.si_index_max'),
            'insert_size.max' => trans('product.product.insert_size_max'),
            'target_yield.max' => trans('product.product.target_yield_max'),
            'qc_requirements.max' => trans('product.product.qc_requirements_max'),
            'sequence_platform.max' => trans('product.product.sequence_platform_max'),
            'sequence_type.max' => trans('product.product.sequence_type_max'),
            'sequence_length.max' => trans('product.product.sequence_length_max'),
            'client_index.max' => trans('product.product.client_index_max'),
            'phix.max' => trans('product.product.phix_max'),
            'delivery_method.max' => trans('product.product.delivery_method_max'),
            'data_analysis.max' => trans('product.product.data_analysis_max'),
            'analysis_details.max' => trans('product.product.analysis_details_max'),
            'flux.max' => trans('product.product.flux_max'),
            'discount.max' => trans('product.product.discount_max'),
            'product_fee.max' => trans('product.product.product_fee_max'),
            'way.max' => trans('product.product.way_max'),
        ];
    }
}
